﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Data.SqlClient;

using MPR.Poker.Model.SYS;
using Library.AppWeb;

namespace MPR.Poker.DALEntity.SYS
{
    /// <summary>
    /// Trần Hòa Nghĩa
    /// 08/12/2013
    /// Cấu hình định dạng lưới
    /// </summary>
    public class DAL_DisplayConfig
    {
        public DAL_DisplayConfig()
        {
        }

        #region Insert/Update

        public ResultMessage UpdateDisplayConfig(List<SYS_DisplayConfig> lst)
        {
            ResultMessage objResultMessage = new ResultMessage();
            try
            {                
                using (PokerLife db = new PokerLife())
                {
                    foreach (SYS_DisplayConfig obj in lst)
                    {
                        if (!obj.IsNewCol)
                        {
                            SYS_DisplayConfig objDisplayConfig = db.SYS_DisplayConfigs.FirstOrDefault(i => i.IDSys == obj.IDSys);
                            if (objDisplayConfig != null && objDisplayConfig.IDSys > 0)
                            {
                                objDisplayConfig.LanguageIDSys = obj.LanguageIDSys;
                                objDisplayConfig.TableName = obj.TableName;
                                objDisplayConfig.OrderIndex = obj.OrderIndex;
                                objDisplayConfig.IsVisible = obj.IsVisible;
                                objDisplayConfig.ColumnName = obj.ColumnName;
                                objDisplayConfig.DisplayName = obj.DisplayName;
                                objDisplayConfig.DataFormat = obj.DataFormat;
                                objDisplayConfig.ColumnWidth = obj.ColumnWidth;
                                objDisplayConfig.HeaderAlign = obj.HeaderAlign;
                                objDisplayConfig.DataAlign = obj.DataAlign;
                                objDisplayConfig.TextColor = obj.TextColor;
                                objDisplayConfig.ColumnType = obj.ColumnType;
                                objDisplayConfig.Method = obj.Method;
                                objDisplayConfig.IsEdit = obj.IsEdit;

                                db.SaveChanges();
                            }
                        }
                        else
                        {
                            SYS_DisplayConfig objDisplayConfig = new SYS_DisplayConfig();
                            objDisplayConfig.LanguageIDSys = obj.LanguageIDSys;
                            objDisplayConfig.TableName = obj.TableName;
                            objDisplayConfig.OrderIndex = obj.OrderIndex;
                            objDisplayConfig.IsVisible = obj.IsVisible;
                            objDisplayConfig.ColumnName = obj.ColumnName;
                            objDisplayConfig.DisplayName = obj.DisplayName;
                            objDisplayConfig.DataFormat = obj.DataFormat;
                            objDisplayConfig.ColumnWidth = obj.ColumnWidth;
                            objDisplayConfig.HeaderAlign = obj.HeaderAlign;
                            objDisplayConfig.DataAlign = obj.DataAlign;
                            objDisplayConfig.TextColor = obj.TextColor;
                            objDisplayConfig.ColumnType = obj.ColumnType;
                            objDisplayConfig.Method = obj.Method;
                            objDisplayConfig.IsEdit = obj.IsEdit;

                            db.SYS_DisplayConfigs.Add(objDisplayConfig);
                            db.SaveChanges();
                        }
                    }
                }
            }
            catch (Exception objEx)
            {
                objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.Insert, "Cập nhật thông tin điều chỉnh hiển thị", objEx);
                DAL_Error.Insert(objResultMessage, 0, "DAL_DisplayConfig --> UpdateDisplayConfig");
                return objResultMessage;
            }
            return new ResultMessage(false, ResultMessage.ErrorTypes.No_Error, "Cập nhật DisplayConfig thành công", string.Empty);
        }

        #endregion

        #region Load/Search/Select
        public ResultMessage SelectDisplayConfigList(ref List<SYS_DisplayConfig> lst, string TableName, int LanguageIDSys)
        {
            ResultMessage objResultMessage = new ResultMessage();
            try
            {
                using (PokerLife db = new PokerLife())
                {
                    
                    lst = db.Database.SqlQuery<SYS_DisplayConfig>("sp_GetDisplayConfig  @TableName, @LanguageIDSys",
                        new SqlParameter("TableName", TableName),
                        new SqlParameter("LanguageIDSys", LanguageIDSys)).ToList();
                }
            }
            catch (Exception objEx)
            {
                objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.Insert, "Lấy thông tin DisplayConfig", objEx);
                DAL_Error.Insert(objResultMessage, 0, "DAL_DisplayConfig --> SelectDisplayConfigList");
                return objResultMessage;
            }
            return new ResultMessage(false, ResultMessage.ErrorTypes.No_Error, "Lấy danh sách DisplayConfig thành công", string.Empty);
        }
        #endregion
    }
}
